﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;

namespace SEO.Utility
{
    /// <summary>
    /// RASHelper 的摘要说明。
    /// </summary>
    public class RASHelper
    {
        public RASHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        const int INTERNET_RAS_INSTALLED = 0x10;
        const int INTERNET_CONNECTION_LAN = 0x02;

        [DllImport("WININET", CharSet = CharSet.Auto)]
        public static extern bool InternetGetConnectedState(
         ref int lpdwFlags,
         int dwReserved);
        [DllImport("WININET", CharSet = CharSet.Auto)]
        public static extern int InternetGetConnectedStateEx(
         ref int lpdwFlags,
         string lpszConnectionName,
         int dwNameLen,
         int dwReserved);

        const int INTERNET_DIALSTATE_DISCONNECTED = 1;
        const int INTERNET_AUTODIAL_FORCE_ONLINE = 1;
        const int INTERNET_AUTODIAL_FORCE_UNATTENDED = 2;
        const int INTERNET_DIAL_UNATTENDED = 0x8000;

        [DllImport("WININET", CharSet = CharSet.Auto)]
        public static extern int InternetDial(
         IntPtr hwndParent,
         string lpszConnectoid,
         int dwFlags,
         ref int lpdwConnection,
         int dwReserved
        );

        const int MAX_PATH = 260;
        const int RAS_MaxDeviceType = 16;
        const int RAS_MaxPhoneNumber = 128;
        const int RAS_MaxEntryName = 256;
        const int RAS_MaxDeviceName = 128;

        const int RAS_Connected = 0x2000;

        [DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int RasEnumConnections(
         ref RASCONN lprasconn,
         ref int lpcb,
         ref int lpcConnections);

        [DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int RasGetConnectStatus(
         IntPtr hrasconn,
         ref RASCONNSTATUS lprasconnstatus);
        [DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int RasHangUp(IntPtr hrasconn);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct RASCONN
        {
            public int dwSize;
            public IntPtr hrasconn;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
            public string szEntryName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceType + 1)]
            public string szDeviceType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceName + 1)]
            public string szDeviceName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
            public string szPhonebook;
            public int dwSubEntry;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct RASCONNSTATUS
        {
            public int dwSize;
            public int rasconnstate;
            public int dwError;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceType + 1)]
            public string szDeviceType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceName + 1)]
            public string szDeviceName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPhoneNumber + 1)]
            public string szPhoneNumber;
        }
        /// <summary>
        /// 关闭拨号连接
        /// </summary>

        public static void HangUp()
        {
            IntPtr m_ConnectedRasHandle;
            RASCONN lprasConn = new RASCONN();

            lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
            lprasConn.hrasconn = IntPtr.Zero;
            int lpcb = 0;
            int lpcConnections = 0;
            int nRet = 0;
            lpcb = Marshal.SizeOf(typeof(RASCONN));


            nRet = RasEnumConnections(ref lprasConn, ref lpcb, ref lpcConnections);


            if (nRet != 0)
            {
                return;

            }
            if (lpcConnections > 0)
            {
                RASCONNSTATUS rasconnstatus = new RASCONNSTATUS();
                m_ConnectedRasHandle = lprasConn.hrasconn;
                RasHangUp(m_ConnectedRasHandle);
                Thread.Sleep(1000);
                for (; ; )
                {
                    rasconnstatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS));
                    if (RasGetConnectStatus(m_ConnectedRasHandle, ref rasconnstatus) == 0)       //获得当前连接的状态
                    {
                        if (rasconnstatus.rasconnstate == 0x2001) //已经彻底挂断
                            break;
                    }
                    else
                        break;//获得当前连接状态失败 
                }
            }

        }
        ///<sumarry>
        ///判断是否是lan连接
        ///</sumarry>
        public static bool IsNetConnectViaLAN()
        {
            int dwFlags = 0;
            return InternetGetConnectedState(ref dwFlags, 0);
            //return dwFlags==INTERNET_CONNECTION_LAN;
        }
    }
}
